package study.week05.main;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import study.week05.componentscan.DemoService;
import study.week05.config.BeanConfig;
import study.week05.config.ConditionBeanConfig;
import study.week05.entity.School;
import study.week05.entity.Student;
@SpringBootApplication(scanBasePackages = "study.week05.autoconfig")
public class SpringBeanTest {

    public static void main(String[] args) {


        // 读xml方式
        useXml();
        // java配置(注解配置)方式
        useJavaConfig();
        // 扫描基础包+注解的方式
        useComponentScan();
        // java配置(注解配置)结合Condition方式
        useJavaConfigWithCondition();
        // springboot结合profile方式
        useSpringbootWithProfile();
    }

    static void useXml(){
        System.out.println("========================================");
        System.out.println("读xml方式:");
        ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");
        School school= context.getBean(School.class);
        Student student = context.getBean(Student.class);
        System.out.println(school);
        System.out.println(student);

    }
    static void useJavaConfig(){
        System.out.println("========================================");
        System.out.println("java配置(注解配置)方式:");
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        School school= context.getBean(School.class);
        Student student = context.getBean(Student.class);
        System.out.println(school);
        System.out.println(student);
    }
    static void useComponentScan(){
        System.out.println("========================================");
        System.out.println("扫描基础包+注解的方式:");
        ApplicationContext context = new AnnotationConfigApplicationContext("study.week05.componentscan");
        DemoService service= context.getBean(DemoService.class);
        service.bizMethod();

    }
    static void useJavaConfigWithCondition(){
        System.out.println("========================================");
        System.out.println("java配置(注解配置)结合Condition方式:");
        ApplicationContext context = new AnnotationConfigApplicationContext(ConditionBeanConfig.class);
        if(context.containsBean("studentBase")){
            Student studentBase = (Student) context.getBean("studentBase");
            System.out.println(studentBase);
        }else{
            System.out.println("不存在studentBase");
        }
        if(context.containsBean("studentCondition")){
            Student studentCondition = (Student) context.getBean("studentCondition");
            System.out.println(studentCondition);
        }else{
            System.out.println("不存在studentCondition");
        }

    }
    static void useSpringbootWithProfile(){
        System.out.println("========================================");
        System.out.println("springboot结合profile方式:");
        ConfigurableApplicationContext context= new SpringApplicationBuilder(SpringBeanTest.class)
                .web(WebApplicationType.NONE).run();
        if(context.containsBean("studentBase")){
            Student studentBase = (Student) context.getBean("studentBase");
            System.out.println(studentBase);
        }else{
            System.out.println("不存在studentBase");
        }
        //--spring.profiles.active=test
        //只有配置了Profile为test才可以获取到这个bean
        if(context.containsBean("studentProfile")){
            Student studentBase = (Student) context.getBean("studentProfile");
            System.out.println(studentBase);
        }else{
            System.out.println("不存在studentProfile");
        }
    }


}
